package com.gouaixin.bee.beeframework2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.gouaixin.bee.beeframework2.R;
//import com.gouaixin.bee.beeframework2.model.SmsModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;

import cn.bmob.im.BmobUserManager;
import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Administrator on 2015/3/5.
 */
//登录界面
public class A3_LoginActivity extends Activity {
    Context context = A3_LoginActivity.this;

    public static String objectID;//保存用户的id
    public static String objectName;//保存用户的昵称

    TextView title;//标题
    ImageView back;//返回


    Button login;//登录
    TextView regin;//免费注册

    String username;
    String password;

    EditText usernameET;
    EditText passwordET;

    BmobUserManager mBmobUserManager;
    BmobChatUser currentUser;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a3_login);


        initHead();
        initContent();

    }

    private void initContent() {
        usernameET = (EditText) findViewById(R.id.username);
        passwordET = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.a3_login);
        regin = (TextView) findViewById(R.id.a3_regin);

        //读取本地保存的id和password
        username = PreferencesUtils.getString(context, "cid", "");

        password = PreferencesUtils.getString(context, "password", "");

        //将本地id和password显示在编辑框
        usernameET.setText(username);
        passwordET.setText(password);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();


            }
        });
//
//

        regin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                android.util.Log.e("Demo", "进入到注册界面");

                Intent mainIntent = new Intent().setClass(A3_LoginActivity.this, A2_ReginActivity.class);
                startActivity(mainIntent);
                finish();


            }
        });

    }

    //登录的主方法
    private void login() {
        //获取编辑框数据
        username = usernameET.getText().toString();
        password = passwordET.getText().toString();
        android.util.Log.e("Demo", "提交登录数据:cid=" + username + "\tpassword=" + password);

        mBmobUserManager = BmobUserManager.getInstance(context);
        mBmobUserManager.login(username, password, new SaveListener() {
            @Override
            public void onSuccess() {
                loginSuccess();
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(context, "登录失败：账户或密码错误", Toast.LENGTH_SHORT).show();
                mLog.e("登录失败11111" + s);
            }
        });
    }

    //登录成功
    private void loginSuccess() {

        //获取当前登录用户信息
        currentUser = mBmobUserManager.getCurrentUser();
        Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();

        objectName = currentUser.getUsername();
        objectID = currentUser.getObjectId();

        //本地保存id和password
        PreferencesUtils.putString(context, "cid", username);

        PreferencesUtils.putString(context, "password", password);


        String icon = PreferencesUtils.getString(context, "icon", "");
//        //如果头像存在
//        if (!icon.equals("")) {
            android.util.Log.e("Demo", "进入到主界面");
            Intent mainIntent = new Intent().setClass(A3_LoginActivity.this, MyMainActivity.class);
            startActivity(mainIntent);
            finish();
//
//
//        }
//        //如果头像不存在
//        else {
//            android.util.Log.e("Demo", "进入到完善资料界面");
//            Intent mainIntent = new Intent().setClass(A3_LoginActivity.this, B12_wanshanZiliaoActivity.class);
//            startActivity(mainIntent);
//            finish();
//
//        }
    }

    //登录失败
    private void loginFailure() {

    }


    private void initHead() {

        back = (ImageView) findViewById(R.id.back);
        Resources resources = getBaseContext().getResources();
        Drawable drawable = resources.getDrawable(R.drawable.home_search_left);
        back.setBackgroundDrawable(drawable);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                android.util.Log.e("Demo", "返回到登录注册界面");

                Intent mainIntent = new Intent().setClass(A3_LoginActivity.this, A1_SigninActivity.class);
                startActivity(mainIntent);
                finish();

            }
        });
    }
}
